package cn.ffcs.sort.dao;

import java.sql.ResultSet;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.sort.model.ProductSort;

/**
 * 字典类型DAO实现<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.huangjx
 * @version $Id$
 */
public class ProductSortDAO<D extends ProductSort> extends BaseDAO<D> implements
        IProductSortDAO<D>
{
    /**
     * 获取字典类型实体类
     * @param b
     * @return
     */
    public String getEntityName()
    {
        return new ProductSort().getClass().getName();
    }
    /**
     * 查询产品，潘宇修改
     * @param b
     * @return
     */
    public ResultSet getResultSetById(String sortId)
    {
        java.sql.Connection conn = null;
        java.sql.Statement stmt = null;
        Session session = null;
        try
        {
            session = super.getSession();
            conn = session.connection();
            stmt = conn.createStatement();
            return stmt
                    .executeQuery("select d.id,d.name,d.parent_sort,(case when d.sort_type=1 "
                            +"then '集团产品目录' else  '福建省产品目录' end) sort_name "
                            +"from T_BUSI_PRODUCT_SORT d start with id='"
                            + sortId + "' connect by d.id=prior d.parent_sort ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            if (session != null)
            {
                releaseSession(session);
            }
        }
    }

    /**
     * 取一定数据
     * @param num
     * @return
     */
    public String findAllSortById(String sortId)
    {
        java.sql.ResultSet rs = null;
        String str = "";
        try
        {
            String pix = "";
            rs = this.getResultSetById(sortId);
            while (rs != null && rs.next())
            {
                str = rs.getString("name") + " -> " + str;
                pix = rs.getString("sort_name");
            }
            if (pix != null && !"".equals(pix)) str = pix + ":" + str;

            if (str.length() > 4)
            {
                str = str.substring(0, str.length() - 4);
            }
            return str;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 取一定数据
     * @param num
     * @return
     */
    public List findProductSortById(String sortId)
    {
        java.sql.ResultSet rs = null;
        List list = new ArrayList();
        try
        {
            String pix = "";
            rs = this.getResultSetById(sortId);
            if (rs != null)
            {
                while (rs.next())
                {
                    list.add(rs.getString("id"));
                }

            }
            return list;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
